﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ADV_Evaluator;
using ADV_Evaluator.Command;
using System.IO;

namespace ADV_Editor.CommandDialog
{
	/// <summary>
	/// クリッカブル領域の配置
	/// </summary>
	/// [2008/05/08] Luice 新規作成
	partial class ClickableMapDialog : CommandDialogBase
	{
		/// <summary>
		/// 初期値を指定しないコンストラクタ
		/// </summary>
		public ClickableMapDialog()
		{
			// コンポーネントの初期化
			InitializeComponent();
			// 画像/ボタン フォルダ下にあるすべての画像をListBoxに表示する
			foreach (string str in Directory.GetFiles("画像/ボタン"))
			{
				string filename = Path.GetFileName(str);
				string ext = Path.GetExtension(str).ToLower();
				foreach (string _ext in Const.ImageExtensions)
				{
					if (ext == _ext)
					{
						lstNormal.Items.Add(filename);
						lstPush.Items.Add(filename);
						lstHover.Items.Add(filename);
						lstDisable.Items.Add(filename);
						break;
					}
				}
			}
			// １つも要素がなければ選択できない
			if (lstNormal.Items.Count == 0)
			{
				lstNormal.Enabled = false;
				lstPush.Enabled = false;
				lstHover.Enabled = false;
				lstDisable.Enabled = false;
				this.grpMode.Enabled = false;
				this.txtLabel.Enabled = false;
				this.txtScenario.Enabled = false;
				this.listBox.Enabled = false;
				button_OK.Enabled = false;
			}
			else
			{
				lstNormal.SelectedIndex = 0;
				lstPush.SelectedIndex = 0;
				lstHover.SelectedIndex = 0;
				lstDisable.SelectedIndex = 0;
			}

			// シナリオ フォルダ下にあるサウンドファイルをすべてListBoxに表示する
			foreach (string str in Directory.GetFiles("シナリオ/"))
			{
				string filename = Path.GetFileName(str);
				string ext = Path.GetExtension(str).ToLower();
				if (ext == Const.ScenarioExtension)
				{
					listBox.Items.Add(filename);
				}
			}

			// １つも要素がなければ選択できない
			if (listBox.Items.Count == 0)
			{
				listBox.Enabled = false;
				radScenario.Enabled = false;
				radLabel.Checked = true;
			}
			else
			{
				// リストボックス設定
				listBox.SelectedIndex = 0;
			}

			this.radLabel.Checked = true;
			this.listBox.Enabled = false;
		}

		/// <summary>
        /// 初期値を指定したコンストラクタ
        /// </summary>
        /// <param name="param">属性</param>
		public ClickableMapDialog(ClickableMapParams param)
			: this()
        {
            switch(param.Mode){
            case ClickableMapMode.Scenario:
				this.radScenario.Checked = true;
				if (listBox.Items.Count > 0 && param.ScenarioFileName.Length > 0)
				{
					listBox.SelectedItem = param.ScenarioFileName;
				}
                break;
			case ClickableMapMode.Label:
				this.radLabel.Checked = true;
				this.txtScenario.Text = param.ScenarioFileName;
                break;
			}
			if (lstNormal.Items.Count > 0 && param.ImageFileNameNormal.Length > 0)
			{
				lstNormal.SelectedItem = param.ImageFileNameNormal;
			}
			if (lstPush.Items.Count > 0 && param.ImageFileNamePush.Length > 0)
			{
				lstPush.SelectedItem = param.ImageFileNamePush;
			}
			if (lstHover.Items.Count > 0 && param.ImageFileNameHover.Length > 0)
			{
				lstHover.SelectedItem = param.ImageFileNameHover;
			}
			if (lstDisable.Items.Count > 0 && param.ImageFileNameDisable.Length > 0)
			{
				lstDisable.SelectedItem = param.ImageFileNameDisable;
			}
			this.txtLabel.Text = param.Label;
			this.numericUpDown_ImageX.Value = param.X;
			this.numericUpDown_ImageY.Value = param.Y;

			switch (param.DeleteMode)
			{
				case DeleteClickableMapMode.None:
					this.radNone.Checked = true; break;
				case DeleteClickableMapMode.Single:
					this.radSimple.Checked = true; break;
				case DeleteClickableMapMode.All:
					this.radAll.Checked = true; break;
			}
			this.txtDeleteLabel.Text = param.DeleteLabel;

        }

		/// <summary>
		/// 設定された画像をコマンド化したもの
		/// </summary>
		public override CommandListData Command
		{
			get
			{
				string filenameNormal = lstNormal.SelectedItem as string;
				// Noneならファイル名を空文字列にしておく
				if (filenameNormal == null || filenameNormal == "(None)")
				{
					filenameNormal = "";
				}
				string filenamePush = lstPush.SelectedItem as string;
				// Noneならファイル名をNormalと同じにしておく
				if (filenamePush == null || filenamePush == "(None)")
				{
					filenamePush = filenameNormal;
				}
				string filenameHover = lstHover.SelectedItem as string;
				// Noneならファイル名をNormalと同じにしておく
				if (filenameHover == null || filenameHover == "(None)")
				{
					filenameHover = filenameNormal;
				}
				string filenameDisable = lstDisable.SelectedItem as string;
				// Noneならファイル名をNormalと同じにしておく
				if (filenameDisable == null || filenameDisable == "(None)")
				{
					filenameDisable = filenameNormal;
				}
				string filename = listBox.SelectedItem as string;
				if (filename == null || filename == "(None)")
				{
					filename = "";
				}
				// 属性
				ClickableMapParams param = new ClickableMapParams();
				param.Label = this.txtLabel.Text;

				if (this.radScenario.Checked)
				{ 
					param.Mode = ClickableMapMode.Scenario;
					param.ScenarioFileName = filename;
				}
				else
				{
					param.Mode = ClickableMapMode.Label; 
					param.ScenarioFileName = this.txtScenario.Text;
				}
				param.ImageFileNameNormal = filenameNormal;
				param.ImageFileNamePush = filenamePush;
				param.ImageFileNameHover = filenameHover;
				param.ImageFileNameDisable = filenameDisable;

				param.X = (int)numericUpDown_ImageX.Value;
				param.Y = (int)numericUpDown_ImageY.Value;

				param.DeleteLabel = this.txtDeleteLabel.Text;
				if (this.radNone.Checked) { param.DeleteMode = DeleteClickableMapMode.None; }
				if (this.radSimple.Checked) { param.DeleteMode = DeleteClickableMapMode.Single; }
				if (this.radAll.Checked) { param.DeleteMode = DeleteClickableMapMode.All; }

				return new CommandListData(CommandName.クリッカブル領域の配置, param);
			}
		}

		private void radMode_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radLabel.Checked)
			{ 
				this.txtScenario.Enabled = true;
				this.txtScenario.Text = string.Empty;
				this.listBox.Enabled = false;
			}
			if (this.radScenario.Checked)
			{
				this.txtScenario.Enabled = false;
				this.txtScenario.Text = string.Empty;
				this.listBox.Enabled = true;
				this.listBox.SelectedIndex = 0;
			}
		}

		private void radDelete_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radNone.Checked)
			{
				this.txtDeleteLabel.Enabled = false;
				this.txtDeleteLabel.Text = string.Empty;
			}
			if (this.radSimple.Checked)
			{
				this.txtDeleteLabel.Enabled = true;
				this.txtDeleteLabel.Text = string.Empty;
			}
			if (this.radAll.Checked)
			{
				this.txtDeleteLabel.Enabled = false;
				this.txtDeleteLabel.Text = string.Empty;
			}
		}

	}
}
